// $URL: http://mingw-lib.googlecode.com/svn/trunk/working/twitpic/widget.cpp $
// $Rev: 72 $
// $Author: akio.miyoshi $
// $Date:: 2010-05-11 07:08:02 +0000#$

#include "widget.h"
#include "ui_widget.h"
#include <QtCore>
#include <QtGui>
#include <QtNetwork>
#include <QtXml>

//#include "xmlutil.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QWidget::setWindowFlags(Qt::WindowStaysOnTopHint); // top most
    nam = new NetworkAccessManager();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Widget::setDesktopFile(QFile &file, QString name)
{
    QString dirPath =
            QDesktopServices::storageLocation(
                    QDesktopServices::DesktopLocation);
    QDir dir(dirPath);
    QString filePath = dir.filePath(name);
    file.setFileName(filePath);
}

void Widget::saveDesktopFile(QString name, QByteArray bytes)
{
    QFile file;
    setDesktopFile(file, name);
    qDebug() << file.fileName();
    file.open(QIODevice::WriteOnly | QIODevice::Truncate);
    file.write(bytes);
}

void Widget::on_pushButton_clicked()
{
    this->ui->pushButton->setEnabled(false);
    QSettings settings(
            QSettings::IniFormat, //QSettings::NativeFormat,
            QSettings::UserScope, // QSettings::SystemScope,
            QCoreApplication::organizationName(),
            QCoreApplication::applicationName()
            );
    qDebug() << settings.value("twitpic/username").toString();
    qDebug() << settings.value("twitpic/password").toString();
    QUrl v_target;
    //v_target.setEncodedUrl("http://twitpic.com/api/uploadAndPost");
    v_target.setEncodedUrl("http://twitpic.com/api/upload");

    QNetworkRequest v_request;
    v_request.setUrl(v_target);
    v_request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=7d44e178b0434");

    QByteArray v_username = settings.value("twitpic/username").toString().toLatin1();
    QByteArray v_password = settings.value("twitpic/password").toString().toLatin1();

    QFile file;
    file.setFileName("../thumb.jpg");
    qDebug() << file.fileName();
    file.open(QIODevice::ReadOnly);
    QByteArray thumb_binary = file.readAll();

    const char *NR="\n";
    QByteArray postData;
    QBuffer postBuffer;
    postBuffer.setBuffer(&postData);
    postBuffer.open(QIODevice::WriteOnly);

    postBuffer.write("--7d44e178b0434");
    postBuffer.write(NR);

    postBuffer.write("Content-Disposition: form-data; name=\"username\"");
    postBuffer.write(NR);
    postBuffer.write("Content-Type: text/plain;charset=UTF-8");
    postBuffer.write(NR);
    postBuffer.write(NR);
    postBuffer.write(v_username);
    postBuffer.write(NR);

    postBuffer.write("--7d44e178b0434");
    postBuffer.write(NR);

    postBuffer.write("Content-Disposition: form-data; name=\"password\"");
    postBuffer.write(NR);
    postBuffer.write("Content-Type: text/plain;charset=UTF-8");
    postBuffer.write(NR);
    postBuffer.write(NR);
    postBuffer.write(v_password);
    postBuffer.write(NR);

    postBuffer.write("--7d44e178b0434");
    postBuffer.write(NR);

    postBuffer.write("Content-Disposition: form-data; name=\"media\"; filename=\"");
    postBuffer.write(file.fileName().toUtf8());
    postBuffer.write("\"");
    postBuffer.write(NR);
    postBuffer.write("Content-Type: image/jpeg");
    postBuffer.write(NR);
    postBuffer.write("Content-Transfer-Encoding: binary");
    postBuffer.write(NR);
    postBuffer.write(NR);
    postBuffer.write(thumb_binary);
    postBuffer.write(NR);

    postBuffer.write("--7d44e178b0434--");
    postBuffer.write(NR);

    postBuffer.close();

    qDebug() << postData;

    QByteArray v_result = nam->doPost(v_request, postData);
    qDebug() << v_result;
    this->ui->pushButton->setEnabled(true);
}
